from enum import Enum
import argparse, ipaddress, json, os

from util import is_file_in_temp

class AiApi(Enum):
    DEEPSEEK_CHAT = "deepseek", "deepseek-chat", "https://api.deepseek.com"
    DEEPSEEK_REASONOR = "deepseek-reasoner", "deepseek-reasoner", "https://api.deepseek.com"
    DOUBAO_SEED = "doubao-seed", "doubao-seed-1-6-250615", "https://ark.cn-beijing.volces.com/api/v3"
    DOUBAO_DS_V3 = "doubao-ds-v3", "deepseek-v3-250324", "https://ark.cn-beijing.volces.com/api/v3"
    DOUBAO_DS_R1 = "doubao-ds-r1", "deepseek-r1-250528", "https://ark.cn-beijing.volces.com/api/v3"

AI_APIS = [ai_api.value[0] for ai_api in AiApi]
AI_API_MODELS = {ai_api.value[0]: ai_api.value[1] for ai_api in AiApi}
AI_API_URLS = {ai_api.value[0]: ai_api.value[2] for ai_api in AiApi}

def str_to_bool(value: str) -> bool:
    # 同上定义的转换函数
    if isinstance(value, bool):
        return value
    value = value.strip().lower()
    if value in ('true', 't', 'yes', 'y', '1'):
        return True
    elif value in ('false', 'f', 'no', 'n', '0'):
        return False
    else:
        return False
PARSER = argparse.ArgumentParser()
PARSER.add_argument('-e', '--env', type=str, default='loc', help='设置应用环境 (loc, dev, pre, pro)')
# PARSER.add_argument('-d', '--debug-key', type=str, default=None, help='输入KEY启用调试日志')
PARSER.add_argument('-n', '--output-name', type=str, default=None, help='指定输出名称')
PARSER.add_argument('-o', '--output-dir', type=str, default=None, help='指定输出目录')
PARSER.add_argument('-r', '--res-file', type=str, default=None, help='指定结果文件')
PARSER.add_argument('--res-save', type=str_to_bool, default=False, help='是否保存结果文件')
PARSER.add_argument('-s', '--subject', type=str, default=None, help='主题')
PARSER.add_argument('-a', '--api', type=str, choices=AI_APIS, default=AiApi.DEEPSEEK_CHAT.value[0], help='AI API接口选择(DeepSeek,Doubao等)')
PARSER.add_argument('-k', '--api-key', type=str, default=None, help='AI API KEY(DeepSeek,Doubao等)')
PARSER.add_argument('--files', type=str, default=None, nargs='*', help='包含文件列表')
ARGS = PARSER.parse_args()

APP_ENV = ARGS.env
print(f'env: {APP_ENV}')

# inc
DEBUG = False
DEBUG_KEY = 'dp123456'
CWD_DIR = os.getcwd() # 返回当前工作目录的绝对路径
print(f"cwd: {CWD_DIR}")
# print(f"当前工作目录: {CWD_DIR}")
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
BASE_DIR_IN_TEMP = is_file_in_temp(BASE_DIR)
print(f"temp: {BASE_DIR_IN_TEMP}")
LOGS_DIR = f'{os.sep}opt{os.sep}logs'
DATA_DIR = f'{os.sep}opt{os.sep}data'

APP_NAME = 'dp'
APP_PRIVATE_KEY = ""
APP_PUBLIC_KEY = ""
APP_ALGORITHM = "RS256"
APP_TOKEN_LITETIME = 86400
APP_SCHEDULER_THREAD_COUNT = 10
APP_SCHEDULER_PROCESS_COUNT = 4
APP_HOST = '0.0.0.0'
APP_PORT = 12380
OUTPUT_NAME = 'out'
AI_API = ""
AI_API_KEY = ""
MAX_TOKENS = 3000
SUBJECT = ""
OUTPUT_DIR = f"{CWD_DIR if BASE_DIR_IN_TEMP else BASE_DIR}{os.sep}out"
RESPONSE_FILE = f"{OUTPUT_DIR}{os.sep}response.json"
MIDDLE_FILE = f"{OUTPUT_DIR}{os.sep}{OUTPUT_NAME}.middle"
DOCPRESS_FILE = f"{OUTPUT_DIR}{os.sep}{OUTPUT_NAME}.xml"

# loc
LOGS_DIR = f'{CWD_DIR if BASE_DIR_IN_TEMP else BASE_DIR}{os.sep}logs'
DATA_DIR = f'{CWD_DIR if BASE_DIR_IN_TEMP else BASE_DIR}{os.sep}data'
OUTPUT_DIR = f"{CWD_DIR if BASE_DIR_IN_TEMP else BASE_DIR}{os.sep}out"
RESPONSE_FILE = f"{OUTPUT_DIR}{os.sep}response.json"
MIDDLE_FILE = f"{OUTPUT_DIR}{os.sep}{OUTPUT_NAME}.middle"
DOCPRESS_FILE = f"{OUTPUT_DIR}{os.sep}{OUTPUT_NAME}.xml"

# custom
def get_option_output_name() :
    output_name = OUTPUT_NAME
    if ARGS.output_name :
        output_name = ARGS.output_name
    print(f'output_name: {output_name}')
    return output_name
def get_option_output_dir() :
    output_dir = OUTPUT_DIR
    if ARGS.output_dir :
        output_dir = ARGS.output_dir
        if output_dir[0:1] == '/' :
            pass
        else :
            output_dir = f'{CWD_DIR if BASE_DIR_IN_TEMP else BASE_DIR}{os.sep}{output_dir}'
    print(f'output_dir: {output_dir}')
    return output_dir
def get_option_response_file() :
    res_file = RESPONSE_FILE
    if ARGS.res_file :
        res_file = ARGS.res_file
        if res_file[0:1] == '/' :
            pass
        else :
            res_file = f'{CWD_DIR if BASE_DIR_IN_TEMP else BASE_DIR}{os.sep}{res_file}'
    print(f'response_file: {res_file}')
    return res_file
def get_option_response_save() :
    response_save = False
    if ARGS.res_save :
        response_save = True
    print(f'response_save: {response_save}')
    return response_save
def get_option_middle_file() :
    return f"{OUTPUT_DIR}{os.sep}{OUTPUT_NAME}.middle"
def get_option_docpress_file() :
    return f"{OUTPUT_DIR}{os.sep}{OUTPUT_NAME}.xml"
def get_option_subject() :
    subject = SUBJECT
    if ARGS.subject and ARGS.subject != '':
        subject = ARGS.subject
    print(f'subject: {subject}')
    return subject
def get_option_subject_files() :
    subject_files: list[str] = []
    if ARGS.files and len(ARGS.files) > 0 :
        subject_files = ARGS.files
    for i, f in enumerate(subject_files) :
        if f[0:1] == '/' :
            pass
        else :
            subject_files[i] = f'{BASE_DIR}{os.sep}{f}'
    print(f'subject_files: {subject_files}')
    return subject_files

def get_option_api():
    api = AI_API
    if ARGS.api and ARGS.api != '':
        api = ARGS.api
    print(f'api: {api}')
    return api
def get_option_api_key(): 
    api_key = AI_API_KEY
    if ARGS.api_key and ARGS.api_key != '':
        api_key = ARGS.api_key
    print(f'api_key: {"***" if api_key else ""}')
    return api_key

OUTPUT_NAME = get_option_output_name()
OUTPUT_DIR = get_option_output_dir()
RESPONSE_FILE = get_option_response_file()
RESPONSE_SAVE = get_option_response_save()
# print(f'RESPONSE_FILE: {RESPONSE_FILE}')
MIDDLE_FILE = get_option_middle_file()
DOCPRESS_FILE = get_option_docpress_file()
SUBJECT = get_option_subject()
SUBJECT_FILES = get_option_subject_files()
AI_API = get_option_api()
AI_API_MODEL = AI_API_MODELS.get(AI_API)
AI_API_URL = AI_API_URLS.get(AI_API)
AI_API_KEY = get_option_api_key()
